package com.example.beikeyigou.trolley;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.beikeyigou.R;
import com.example.beikeyigou.utils.URLUtils;
import com.squareup.picasso.Picasso;

import java.util.List;

public class TrolleyAdapter extends BaseAdapter {
    Context context;
    List<TrolleyBean.DataBean> data;
    List<Boolean> bList;

    public List<Boolean> getBList() {
        return bList;
    }

    public interface OnSelectListener{
        public void onSelect(int pos, boolean status);
    }
    OnSelectListener onSelectListener;

    public TrolleyAdapter setOnSelectListener(OnSelectListener onSelectListener) {
        this.onSelectListener = onSelectListener;
        return this;
    }

    public TrolleyAdapter(Context context, List<TrolleyBean.DataBean> data, List<Boolean> bList) {
        this.context = context;
        this.data = data;
        this.bList = bList;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final VH vh ;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_trolley, parent, false);
            vh = new TrolleyAdapter.VH(convertView);
            convertView.setTag(vh);
        } else {
            vh = (TrolleyAdapter.VH) convertView.getTag();
        }
        TrolleyBean.DataBean bean = data.get(position);
        vh.titleTv.setText(bean.getCommodityName());
        vh.numTv.setText("×" + String.valueOf(bean.getCount()));
        vh.priceTv.setText("￥" + String.valueOf(bean.getTotalprices() / bean.getCount()));
        String photo = bean.getCommodityPhoto();
        if (!TextUtils.isEmpty(photo)) {
            String photoUrl = URLUtils.PUBLIC_URL+photo;
            Picasso.with(context).load(photoUrl).into(vh.iv);
        }
        boolean status = bList.get(position);

        vh.checkBox.setChecked(status);
        VH finalVh = vh;
        vh.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                vh.checkBox.setChecked(!status);
                if (onSelectListener != null) {
                    onSelectListener.onSelect(position, !status);
                    notifyDataSetChanged();
                }
            }
        });

        return convertView;
    }

    class VH {
        TextView titleTv, priceTv, numTv;
        ImageView iv;
        CheckBox checkBox;

        public VH(View v) {
            checkBox = v.findViewById(R.id.item_trolley_checkBox);
            titleTv = v.findViewById(R.id.item_trolley_goodTitle);
            priceTv = v.findViewById(R.id.item_trolley_goodPrice);
            numTv = v.findViewById(R.id.item_trolley_goodNum);
            iv = v.findViewById(R.id.item_trolley_goodImg);
        }
    }
}
